package cn.woblog.android.httputils.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class StreamTools {

	/**
	 * 流转化工具类
	 * 
	 * @param is
	 *            输入流
	 * @return 文本字符串
	 */
	public static String readInputStream(InputStream is) {
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = is.read(buffer)) != -1) {
				baos.write(buffer, 0, len);
			}
			is.close();
			byte[] result = baos.toByteArray();
			// 试着用utf-8 编码一下.
			String temp = new String(result);
			if (temp.contains("charset=gb2312")) {
				return new String(result, "gb2312");
			} else if (temp.contains("charset=big-5")) {
				return new String(result, "big-5");
			} else {
				return temp;
			}
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}

	}
}